# -*- coding: utf-8 -*-
# This code written by ChanGOGOGO
"""
呼入任务页面元素
"""
import sys
import os

current_script_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_script_dir)
if parent_dir not in sys.path:
    sys.path.append(parent_dir)
from pages.base_page import BasePage


class CallInTaskPage(BasePage):
    def __init__(self, page):
        super().__init__(page)
        self.create_task_window = self._create_task_window()

    def _create_task_window(self):
        """新建呼入任务窗口"""
        element = self.page.locator(
            "//div[@role='dialog' and @aria-label='新建呼入通话任务']"
        )
        return element

    def choose_scene_placeholder(self):
        """选择场景下拉框"""
        element = self.create_task_window.get_by_role("textbox", name="选择场景")
        return element

    def scene_dropbox_option(self, option):
        """选择场景下拉框可选项，多层级都可以选择"""
        element = self.page.get_by_role("menuitem", name=option)
        return element


if __name__ == "__main__":
    from playwright.sync_api import Playwright, sync_playwright, expect

    def run(playwright: Playwright) -> None:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()
        page.goto("http://172.18.160.155:40080/#/user/login")
        page.get_by_placeholder("AIForce ID").fill("yaocheng1101@wezhuiyi.com")
        page.get_by_placeholder("密码").fill("admin@123")
        page.get_by_placeholder("密码").press("Enter")
        with page.expect_popup() as page1_info:
            page.get_by_role("button", name="弘毅").click()
        page1 = page1_info.value
        page1.get_by_text("呼入任务").click()
        page1.get_by_role("button", name="新建呼入任务").click()
        callin_task_page = CallInTaskPage(page1)
        callin_task_page.choose_scene_placeholder().click()
        callin_task_page.scene_dropbox_option("实验组").click()
        callin_task_page.scene_dropbox_option("CAPS").click()
        callin_task_page.scene_dropbox_option("CAPS-A").click()
        page1.wait_for_timeout(3000)
        callin_task_page.button("保存并启用").click()
        # ---------------------
        context.close()
        browser.close()

    with sync_playwright() as playwright:
        run(playwright)
